package com.zktr.service;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.zktr.bean.GoodsBean;
import com.zktr.dao.GoodsDao;
import com.zktr.dao.GoodsExcelDao;
import com.zktr.pojo.Goods;
import com.zktr.pojo.OrderInfo;
import com.zktr.pojo.OrderInfoBean;
import com.zktr.pojo.PageBean;

public class GoodsService {
	GoodsDao gDao = new GoodsDao();

	/**
	 * 测试所有信息
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		GoodsService goodsService = new GoodsService();
		Goods gBeans = new Goods();
		System.out.println(goodsService.queryOneGoods(1105));
/*		PageBean<GoodsBean> findAllOrders = goodsService.findAllGoods(1, 3);
		System.out.println(findAllOrders.getBeanList());
		gBeans.setGoodsname("小");
		PageBean<GoodsBean> findOrders = goodsService.findGoods(gBeans, 1, 5);
		System.out.println(findOrders.getBeanList());*/
	}

	/**
	 * 查询所有商品
	 * 
	 * @return
	 */
	public Goods queryAllGoods() {
		List<Goods> queryAllGoods = gDao.queryAllGoods();
		for (Goods goods : queryAllGoods) {
			System.out.println(goods);
		}
		return null;
	}

	/**
	 * 查询所有信息，并分页，以及数据类型装换
	 * 
	 * @param pc
	 * @param ps
	 * @return
	 */
	public PageBean<GoodsBean> findAllGoods(int pc, int ps) {
		List<Goods> selectGoods = gDao.selectGoods(pc, ps);
		List<GoodsBean> list2 = new LinkedList<>();
		findAllGoods(selectGoods, list2);
		PageBean<GoodsBean> bean = new PageBean<GoodsBean>();
		bean.setPc(pc);
		bean.setPs(ps);
		bean.setBeanList(list2);
		bean.setTr(gDao.selectGoodsCount());
		return bean;
	}

	/**
	 * 时间类型转换
	 * 
	 * @param addtime
	 * @return
	 */
	private static String mydate(int addtime) {
		Calendar cal = Calendar.getInstance();
		long millions = new Long(addtime).longValue() * 1000;
		cal.setTimeInMillis(millions);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(cal.getTime());
	}

	/**
	 * 更新商品信息
	 * 
	 * @param goods
	 * @param id
	 * @return
	 */
	public int updateGoods(Goods goods, int id) {
		return gDao.updateGoods(goods, id);

	}

	public int addNewGoods(Goods goods) {
		return gDao.insertOneGoods(goods);

	}
	/**
	 * 查询商品：按条件分页查询
	 * @param info
	 * @param pc
	 * @param ps
	 * @return
	 */
	public PageBean<GoodsBean> findGoods(Goods goods, int pc, int ps) {
		Map<Integer, List<Goods>> selectBooks = gDao.selectGoods(goods, pc, ps);
		List<GoodsBean> list2=new LinkedList<>();
		PageBean<GoodsBean> bean=new PageBean<GoodsBean>();
		findAllGoods(selectBooks.get(0), list2);
		bean.setPc(pc);//页码
		bean.setPs(ps);//页大小
		bean.setBeanList(list2);
		bean.setTr(gDao.selectGoodsCount(goods));//总记录条数
		return bean;
	}
	/**
	 * 根据id查询商品信息
	 * @param id
	 * @return
	 */
	public List<GoodsBean> queryOneGoods(int id) {
        GoodsService goodsService = new GoodsService();
        GoodsDao goodsDao = new GoodsDao();
        List<Goods> queryOneGoodsId = goodsDao.queryOneGoodsId(id);
        List<GoodsBean> list2=new ArrayList<>();
        findAllGoods(queryOneGoodsId, list2);
		return list2;
	}
	/**
	 * 类型转换
	 * 
	 * @param list
	 * @param list2
	 */
	private static void findAllGoods(List<Goods> list, List<GoodsBean> list2) {
		for (Goods goods : list) {
			GoodsBean gBeans = new GoodsBean();
			gBeans.setBrandid(goods.getBrandid() + "");
			gBeans.setCatid(goods.getCatid() + "");
			gBeans.setGoodsid(goods.getGoodsid() + "");
			gBeans.setGoodssn(goods.getGoodssn());
			gBeans.setGoodsname(goods.getGoodsname());
			gBeans.setIntegral(goods.getIntegral() + "");
			gBeans.setBrandid(goods.getBrandid() + "");
			gBeans.setGoodsnumber(goods.getGoodsnumber() + "");
			gBeans.setMarketprice(goods.getMarketprice() + "");
			gBeans.setWarnnumber(goods.getWarnnumber() + "");
			gBeans.setKeywords(goods.getKeywords());
			gBeans.setGoodsbrief(goods.getGoodsbrief());
			gBeans.setGoodsdesc(goods.getGoodsdesc());
			gBeans.setGoodsimg(goods.getGoodsimg());
			gBeans.setGiveintegral(goods.getGiveintegral() + "");
			gBeans.setAddtime(mydate(goods.getAddtime()));
			gBeans.setTypeid(goods.getTypeid() + "");
			if (goods.isIsdelete()) {
				gBeans.setIsdelete("../img/yes.gif");
			} else {
				gBeans.setIsdelete("../img/err.gif");
			}
			if (goods.isIsnew()) {
				gBeans.setIsnew("../img/yes.gif");
			} else {
				gBeans.setIsnew("../img/err.gif");
			}
			if (goods.isIshot()) {
				gBeans.setIshot("../img/yes.gif");
			} else {
				gBeans.setIshot("../img/err.gif");
			}

			list2.add(gBeans);
		}

	}

	/**
	 * 按分类分页查询所有的商品
	 * 
	 * @return
	 */
	public PageBean<Goods> queryByCategoryAll(int catid, int pc, int ps) {
		List<Goods> goodsAll = gDao.selectByCategoryAll(catid, pc, ps);
		PageBean<Goods> pagebean = new PageBean<>();
		pagebean.setBeanList(goodsAll);
		pagebean.setPc(pc);
		pagebean.setPs(ps);
		pagebean.setTr(gDao.selectGoodsCount(catid));
		return pagebean;
	}

	/**
	 * 商品批量上传
	 * 
	 * @param url
	 * @return
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public void testFileUpdate(String url) throws FileNotFoundException, IOException {

		GoodsExcelDao gExcelDao = new GoodsExcelDao();
		gExcelDao.enterGoods(url);

	}

	/**
	 * 查询首页秒杀后面的数据
	 * 
	 * @return
	 */
	public List<Goods> queryIndex() {
		return gDao.selectIndex();
	}

	public int deleteOneGoods(int id) {
		return gDao.deleteOneGoods(id);
	}



}
